﻿using System;
using System.Linq;
using System.Net;
using System.Net.Mail;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using Baggr.ClientSite.Models;

namespace Baggr.ClientSite.Controllers
{
    public class AccountController : Controller
    {
        [HttpGet, Authorize]
        public ViewResult Index()
        {
            return View();
        }

        public ActionResult Authenticate()
        {
            return View();
        }

        [HttpPost]
        public ActionResult LogOn(
            string userName,
            string userPassword,
            string returnUrl)
        {
            if (Membership.ValidateUser(userName, userPassword))
            {
                var authCookie = FormsAuthentication.GetAuthCookie(userName, false);
                authCookie.Expires = DateTime.Today.AddYears(100);
                HttpContext.Response.Cookies.Add(authCookie);
            }
            if(!string.IsNullOrEmpty(returnUrl))
                return Redirect(returnUrl);

            return RedirectToAction("Index", "Reader");
        }

        [HttpGet, Authorize]
        public ActionResult LogOut()
        {
            FormsAuthentication.SignOut();
            return RedirectToAction("Index", "Reader");
        }
        [HttpPost]
        public ActionResult Register(UserModel user)
        {
            if (!ModelState.IsValid)
                return Redirect("/");
            Membership.CreateUser(user.Name, user.Password, user.Email);
            return LogOn(user.Name, user.Password, null);
        }
        [HttpPost]
        public ActionResult RemindPassword(string userEmail)
        {
            if (userEmail == null)
                throw new ArgumentNullException("userEmail");

            int recordCount;
            var user = Membership.FindUsersByEmail(userEmail, 0, 100, out recordCount).Cast<MembershipUser>().First();
            if(recordCount != 1)
                throw new HttpException("User using this e-mail isn't found");

            var smtpClient = new SmtpClient(
                Properties.Settings.Default.SmtpServerServer,
                Properties.Settings.Default.SmtpServerPort)
                                 {
                                     EnableSsl = Properties.Settings.Default.SmtpUseSSL
                                 };

            var from = new MailAddress(
                Properties.Settings.Default.SmtpSenderEMail,
                Properties.Settings.Default.SmtpSenderName);

            var to = new MailAddress(userEmail);

            var message = new MailMessage(from, to);

            var newPassword = user.ResetPassword();
            message.Body = "Your password is " + newPassword + ".";

            message.Subject = Properties.Settings.Default.SmtpReminderSubject;

            smtpClient.Credentials = new NetworkCredential(
                Properties.Settings.Default.SmtpSenderEMail,
                Properties.Settings.Default.SmtpSenderPassword,
                string.Empty);

            try
            {
                smtpClient.Send(message);
            }
            catch (Exception e)
            {
                throw new HttpException("Ошибка выполнения запроса", e);
            }
            return Redirect("PasswordSent");
        }

    }
}
